package cn.tomoki.demo01.controller;

import cn.tomoki.demo01.entity.Appoint;
import cn.tomoki.demo01.service.AppointServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AppointController {
    @Autowired
    public AppointServiceImpl appointService;
    ObjectMapper mapper = new ObjectMapper();

    /**
     * @author 仓友树
     * @return
     * 返回所有预约信息
     */
    @GetMapping("/gerAppointAll")
    public String getAppointAll(){
        Collection<Appoint> appoint = appointService.getAppointAll();
        try {
            return mapper.writeValueAsString(appoint);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @author 仓友树
     * @param mark
     * @return
     * 按流水单返回单个预约记录以及对应的产品信息
     */
    @RequestMapping(value = "/getAppointByMark",method = RequestMethod.POST)
    @GetMapping("/getAppointByMark")
    public String getAppointByMark(int mark){
        HashMap<String, Object> map = appointService.getAppointByMark(mark);
        try {
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @author 仓友树
     * @param userId,name,number
     * @return
     * 接收预约信息。如果预约成功，返回true；反之返回false
     */
    @RequestMapping(value = "/addAppoint",method = RequestMethod.POST)
    public boolean addAppoint(String userId, String name, int number){
        Appoint appoint = new Appoint(1, userId, name, number, null,"1");
        int judge = appointService.addAppoint(appoint);
        if (judge > 0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * @author 仓友树
     * @param mark
     * @param state
     * @return
     * 接收预约状态——收货或取消预约
     */
    @RequestMapping(value = "/changeState",method = RequestMethod.POST)
    @GetMapping("/changeState")
    public boolean changeState(int mark,String state){
        int judge = 0;
        switch (state){
            case "2":
                judge = appointService.confirmReceipt(mark);
            case "0":
                judge = appointService.cancelAppoint(mark);
        }
        if (judge > 0){
            return true;
        }else {
            return false;
        }
    }

}
